package com.lhgray.sglhome.presentation.ui.compose

import androidx.compose.foundation.Image
import androidx.compose.foundation.layout.Row
import androidx.compose.foundation.layout.fillMaxSize
import androidx.compose.foundation.layout.padding
import androidx.compose.foundation.layout.size
import androidx.compose.foundation.shape.CircleShape
import androidx.compose.material3.MaterialTheme
import androidx.compose.material3.Surface
import androidx.compose.material3.Text
import androidx.compose.runtime.Composable
import androidx.compose.ui.Alignment
import androidx.compose.ui.Modifier
import androidx.compose.ui.draw.clip
import androidx.compose.ui.draw.shadow
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.graphics.painter.Painter
import androidx.compose.ui.layout.ContentScale
import androidx.compose.ui.res.painterResource
import androidx.compose.ui.tooling.preview.Preview
import androidx.compose.ui.unit.dp
import com.lhgray.sglhome.domain.model.UserData

/**
 * 作者：lhgray on 2025/3/25.
 * 邮箱：lhgray@163.com
 * 文件：com.lhgray.sglhome.screen.compose
 * 用户头像加名称的展示
 */
@Preview(showBackground = true)
@Composable
fun UserHandlePreviewView() {
    UserHandleView(UserData())
}

@Composable
fun UserHandleView(userData: UserData = UserData()) {
    // 顶部用户信息
    Row(
        modifier = Modifier.padding(16.dp, 10.dp), verticalAlignment = Alignment.CenterVertically
    ) {
        val painter = painterResource(id = userData.icon)
        CircularImageWithShadow(painter, modifier = Modifier.size(55.dp))
        Text(
            text = userData.userName,
            style = MaterialTheme.typography.titleLarge,
            modifier = Modifier.padding(start = 16.dp)
        )
    }
}

@Composable
fun CircularImageWithShadow(
    painter: Painter,
    modifier: Modifier = Modifier,
    contentDescription: String? = null,
) {
    Image(
        painter = painter,
        contentDescription = contentDescription,
        contentScale = ContentScale.Crop,
        modifier = modifier
            .size(100.dp)
            .shadow(
                elevation = 8.dp,
                shape = CircleShape,
                ambientColor = Color.Black.copy(alpha = 0.3f),
                spotColor = Color.Black.copy(alpha = 0.3f)
            )
            .clip(CircleShape)
    )
}

// 占位头像组件
@Composable
fun CircleImage(painter: Painter, contentDescription: String, modifier: Modifier = Modifier) {
    Surface(
        shape = CircleShape,
        modifier = modifier
    ) {
        Image(
            painter = painter,
            contentDescription = contentDescription,
            modifier = Modifier.fillMaxSize(),
            contentScale = ContentScale.Crop
        )
    }
}
